﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;

namespace Puzzler.Plugin.Utils
{
    public static class PuzzlerPluginUtilsService
    {
        public static T[][] ConvertToArray2D<T>(IList<Rect> rects) 
        {
            if (rects == null || rects.Count <= 0)
                throw new ArgumentException("Rect list must be set and must contains one element as minimum!");

            int left = int.MaxValue, top = int.MaxValue, right = 0, bottom = 0;
            foreach (Rect rect in rects)
            {
                if (left > rect.Left)
                    left = (int) rect.Left;
                if (top > rect.Top)
                    top = (int) rect.Top;
                if (right < rect.Right)
                    right = (int) rect.Right;
                if (bottom < rect.Bottom)
                    bottom = (int) rect.Bottom;
            }

            T[][] result = new T[right][];
            for (int i = 0; i < result.Length; i++)
            {
                result[i] = new T[bottom];
            }

            for (int i = 0; i < result.Length; i++)
            {
                for (int j = 0; j < result[i].Length; j++)
                {
                    result[i][j] = default(T);
                }
            }

            foreach (Rect rect in rects)
            {
                for (int i = (int) rect.Left; i < rect.Right; i++)
                {
                    for (int j = (int) rect.Top; j < rect.Bottom; j++)
                    {
                        result[i][j] = Activator.CreateInstance<T>();
                    }
                }
            }

            return result;
        }
    }
}
